$(function() {
    $(".u-tabs .tab").click(function() {
        var $this = $(this),
            parent = $this.parents(".u-tabs"),
            tabs = parent.find(".tab-box .tab"),
            cnts = parent.find(".cnt-box .cnt"),
            idx = tabs.index($this),
            oldidx = parent.find(".tab-box .tab.sel").index();

        tabs.removeClass("sel");
        $this.addClass("sel");
        mainCtrl.curTabIdx = idx;
        console.log(mainCtrl.curTabIdx);
        //如果点击了当前选中的状态就不去请求
        if(oldidx == idx) {
            return;
        }
        cnts.hide();
        cnts.eq(idx).show();
    });

    //主流程对象
    var mainCtrl = avalon.define({
        $id: "mainController",
        regionCode: "",
        allRegion: [],
        regionCode2: "",
        //      allRegion: [],
        branchCode: "",
        branchCode2: "",
        allBranch: [],
        storeCode: "",
        storeCode2: "",
        allStore: [],
        taskId: "",
        dataLen: 0,
        dataLen2: 0,
        taskStatus: "",
        searchStatus: "",
        taskCategory: "",
        searchCateogry: "", //导出的时候需要用到查询时选中的品类编码
        taskBrand: "",
        searchBrand: "", //导出的时候需要用到查询时选中的品牌编码
        taskCategory2: "",
        searchCategory2: "",
        allCategoryList: [],
        categoryList: [],
        brandList: [],
        curTabIdx: 0,
        staffId : "",
        returnManage: function() {
            window.location = "taskManage.html";
        },
        search: function(type) {
            if(type == 2) {
                mainCtrl.regionCode2 = $("#magicRegion2 .m-id").val();
                if(!mainCtrl.regionCode2) {
                    return;
                }
                mainCtrl.branchCode = $("#magicBranch2 .m-id").val();
                mainCtrl.storeCode = $("#magicStore2 .m-id").val();
                mainCtrl.searchCateogry = mainCtrl.taskCategory2;
                opts2.settings.searchParam = {
                    "regionCode": mainCtrl.regionCode2,
                    "branchCode": mainCtrl.branchCode2,
                    "storeCode": mainCtrl.storeCode2,
                    "pageNo": 1,
                    "pageSize": 10,
                    "taskId": mainCtrl.taskId,
                    "cateCode": mainCtrl.taskCategory2,
                }
                taskTable2.search(opts2.settings.searchParam);
                var intervalSource2 = setInterval(function() {
                    mainCtrl.dataLen2 = taskTable2.table.total;
                    if(mainCtrl.dataLen2 > 0) {
                        clearInterval(intervalSource2);
                    }
                }, 100);
                return;
            } else if (type == 3) {
            	//  查询指定督导
            	if (mainCtrl.staffId.length < 1) {
            		alert("请输入工号");
            		return;
            	}
            	if (null == taskTable3) {
            		opts3.settings.searchParam.staffId = mainCtrl.staffId;
            		// 初始化表格数据
                    taskTable3 = SwiftUI.swiftTable.getInstance(opts3);
                    return;
            	}
            	var searchParam = {
                    "pageNo": 1,
                    "pageSize": 10,
                    "staffId": mainCtrl.staffId,
                }
            	taskTable3.search(searchParam);
            	return;
            }
            mainCtrl.regionCode = $("#magicRegion .m-id").val();
            if(!mainCtrl.regionCode) {
                return;
            }
            mainCtrl.storeCode = $("#magicStore .m-id").val();
            mainCtrl.branchCode = $("#magicBranch .m-id").val();
            mainCtrl.searchCateogry = mainCtrl.taskCategory;
            mainCtrl.searchBrand = mainCtrl.taskBrand;
            mainCtrl.searchStatus = mainCtrl.taskStatus;
            opts.settings.searchParam = {
                "regionCode": mainCtrl.regionCode,
                "branchCode": mainCtrl.branchCode,
                "pageNo": 1,
                "pageSize": 10,
                "storeCode": mainCtrl.storeCode,
                "taskId": mainCtrl.taskId,
                "taskStatus": mainCtrl.taskStatus,
                "cateCode": mainCtrl.taskCategory,
                "brandCode": mainCtrl.taskBrand
            }
            taskTable.search(opts.settings.searchParam);
            var intervalSource = setInterval(function() {
                mainCtrl.dataLen = taskTable.table.total;
                if(mainCtrl.dataLen > 0) {
                    clearInterval(intervalSource);
                }
            }, 100);

        },
        toExport: function(idx) {
            var exportURL = DI.exportHasAssign + "?index=" + idx + "&taskId=" + mainCtrl.taskId + "&regionCode=" + mainCtrl.regionCode,
                storeCode = mainCtrl.storeCode,
                branchCode = mainCtrl.branchCode,
                brandCode = mainCtrl.searchBrand,
                categoryCode = mainCtrl.searchCateogry,
                status = mainCtrl.searchStatus;
                
            if(mainCtrl.curTabIdx == 0 && status) {
                exportURL += "&taskStatus=" + status;
            }
            if(mainCtrl.curTabIdx == 1) {
                exportURL = DI.exportUnassign + "?index=" + idx + "&taskId=" + mainCtrl.taskId + "&regionCode=" + mainCtrl.regionCode2;
                storeCode = mainCtrl.storeCode2;
                branchCode = mainCtrl.branchCode2;
                categoryCode = mainCtrl.searchCategory2;
            }
            if(storeCode) {
                exportURL += "&storeCode=" + storeCode;
            }
            if(branchCode) {
                exportURL += "&branchCode=" + branchCode;
            }
            if(mainCtrl.curTabIdx == 0 && brandCode) {
                exportURL += "&brandCode=" + brandCode;
            }
            if(categoryCode) {
                exportURL += "&cateCode=" + categoryCode;
            }
            window.location = exportURL;
        },
        exportPop: function(exptype) {
            var dataLen = exptype == 2 ? mainCtrl.dataLen2 : mainCtrl.dataLen;
            if(dataLen == 0) {
                return;
            }
            //如果总数据 小于 获取的导出限制值 说明数据量很小，直接导出 
            if(dataLen <= popCtrl.gotNum) {
                mainCtrl.toExport(1);
                return;
            }
            //0代表没有取过导出限制值，如果取过就不用再去取了
            if(popCtrl.gotNum == 0) {
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: DI.queryExptLimit,
                    success: function(data) {
                        var num = data.data,
                            exportCount = Math.ceil(dataLen / num),
                            countList = [],
                            startNum = 1;
                        popCtrl.gotNum = num;
                        //如果总数据 小于 获取的导出限制值 说明数据量很小，直接导出 
                        if(dataLen <= popCtrl.gotNum) {
                            mainCtrl.toExport(1);
                        } else {
                            for(var i = 0; i < exportCount; i++) {
                                var endNum = startNum + (num - 1);
                                if(endNum > dataLen) {
                                    endNum = dataLen;
                                }
                                countList.push(startNum + "~" + endNum);
                                startNum = endNum + 1;
                            }
                            popCtrl.exportCountList = countList;
                            $("#popup_cnt").show();
                        }
                    },
                    error: function(err) {
                        console.log(err);
                    }
                });
            } else {
                $("#popup_cnt").show();
            }

        },
        searchBrandByCate: function() {
            mainCtrl.taskBrand = "";
            mainCtrl.brandList = [];
            var code = mainCtrl.taskCategory,
                categoryList = mainCtrl.categoryList,
                brandList = [];
            for(var i in categoryList) {
                var category = categoryList[i];
                if(code == category.categoryCode) {
                    brandList = category.brandList;
                    break; //code肯定是唯一的，找到就不需要再找了
                }
            }
            if(code != "" && brandList[0].brandCode != "") {
                brandList.unshift({
                    brandCode: "",
                    brandName: "全部"
                });
            }
            mainCtrl.brandList = brandList;
        }
    });
    //表格实例化
    var taskTable = null;
    var opts = {
        targetId: "taskTable",
        templateId: "tableTask",
        settings: {
            url: DI.queryHasAssignList,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            },
            searchParam: {
                "pageNo": 1,
                "pageSize": 10,
                "storeCode": "",
                "taskId": "",
                "brandCode": "",
                "cateCode": "",
                "disCode": "",
                "orgCode": "",
                "taskStatus": ""
            }
        }
    };
    var taskTable2 = null;
    var opts2 = {
        targetId: "taskTable2",
        templateId: "tableTask2",
        settings: {
            url: DI.queryNoAssignList,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            },
            searchParam: {
                "branchCode": "",
                "pageNo": 1,
                "pageSize": 10,
                "regionCode": "",
                "storeCode": "",
                "taskId": ""
            }
        },
        customFuncs: {
            unallocated: function(row) {
                popCtrl.info.brandNames = row.brandNames ? row.brandNames : "";
                $("#popup_info").show();
            }
        }
    };
    var taskTable3 = null;
    var opts3 = {
        targetId: "taskTable3",
        templateId: "tableTask3",
        settings: {
            url: DI.queryAppointTask,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            },
            searchParam: {
                "staffId": mainCtrl.staffId,
                "pageNo": 1,
                "pageSize": 10
            }
        },
        customFuncs: {
        	clearStaff: function(row) {
        		popCtrl.clearTaskObj = row;
        		$("#popup_confirm").show();
            }
        },
        dataFilter : function(rows){
        	console.log(rows);
        	return rows;
        }
    };
    
    //弹框对象
    var popCtrl = avalon.define({
        $id: "popController",
        gotNum: 0, //判断是否获取过导出限制值
        exportCountList: [],
        countIdx: 1,
        clearTaskObj: {},
        selCount: function() {
            popCtrl.countIdx = $(this).index() + 1;
            $(this).addClass("sel").siblings(".e-number").removeClass("sel");
        },
        exportList: function() {
            mainCtrl.toExport(popCtrl.countIdx);
        },
        info: {
            brandNames: ""
        },
        confirmSure: function() {
        	var dataJson = {
                "taskId" : popCtrl.clearTaskObj.taskId,
                "staffId" : popCtrl.clearTaskObj.staffId,
                "storeCode" : popCtrl.clearTaskObj.storeCode,
                "cateCode" : popCtrl.clearTaskObj.cateCode
            };
            $.ajax({
                type: "post",
                dataType: "json",
                data: dataJson,
                url: DI.clearAppointTask,
                success: function(data) {
                    $("#popup_confirm").hide();
                    if(data.retFlag == 1) {
                        PLATFORM.showTips("success", "删除成功");
                        taskTable3.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                	$("#popup_confirm").hide();
                    console.log(err)
                }
            });
        }
    });
    var magicRegion = $('#magicRegion').magicSuggest({
        renderer: function(city) {
            return city.name;
        },
        placeholder: "选择大区",
        required: true,
        selectionStacked: true,
        expandOnFocus: true,
        selectionRenderer: function(city) {
            return city.name;
        }
    });
    var magicBranch = $('#magicBranch').magicSuggest({
        renderer: function(city) {
            return city.name;
        },
        placeholder: "选择分公司",
        required: true,
        selectionStacked: true,
        expandOnFocus: true,
        selectionRenderer: function(city) {
            return city.name;
        }
    });
    var magicStore = $('#magicStore').magicSuggest({
        renderer: function(city) {
            return city.name;
        },
        placeholder: "选择门店编号/名称",
        required: true,
        selectionStacked: true,
        expandOnFocus: true,
        selectionRenderer: function(city) {
            return city.name;
        }
    });
    var magicRegion2 = $('#magicRegion2').magicSuggest({
        renderer: function(city) {
            return city.name;
        },
        placeholder: "选择大区",
        required: true,
        selectionStacked: true,
        expandOnFocus: true,
        selectionRenderer: function(city) {
            return city.name;
        }
    });
    var magicBranch2 = $('#magicBranch2').magicSuggest({
        renderer: function(city) {
            return city.name;
        },
        placeholder: "选择分公司",
        required: true,
        selectionStacked: true,
        expandOnFocus: true,
        selectionRenderer: function(city) {
            return city.name;
        }
    });
    var magicStore2 = $('#magicStore2').magicSuggest({
        renderer: function(city) {
            return city.name;
        },
        placeholder: "选择门店编号/名称",
        required: true,
        selectionStacked: true,
        expandOnFocus: true,
        selectionRenderer: function(city) {
            return city.name;
        }
    });

    function getAllStores() {
        $(".loadData").show();
        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                "taskId": mainCtrl.taskId
            },
            url: DI.queryOrgTree,
            success: function(data) {
                $(".loadData").hide();
                if(data.retFlag != 1) {
                    console.log("返回retFlag错误" + data.errorMessage);
                    return;
                }
                for(var i = 0; i < data.data.length; i++) {
                    var dataObj = data.data[i];
                    if(dataObj.level == 1) {
                        mainCtrl.allRegion.push(dataObj);
                    } else if(dataObj.level == 2) {
                        mainCtrl.allBranch.push(dataObj);
                    } else if(dataObj.level == 3) {
                        mainCtrl.allStore.push(dataObj);
                    }
                }

                var allRegionData = [];
                for(var i = 0; i < mainCtrl.allRegion.length; i++) {
                    allRegionData.push({
                        id: mainCtrl.allRegion[i].id,
                        name: mainCtrl.allRegion[i].id + "-" + mainCtrl.allRegion[i].text
                    });
                }
                magicRegion.setData(allRegionData);
                magicRegion2.setData(allRegionData);
                $(magicRegion).on(
                    'selectionchange',
                    function(e, cb, s) {
                        var str = cb.getValue(),
                            bcode = $("#magicBranch .m-id").val(),
                            pid = $("#magicBranch .m-pid").val();
                        getBranchByCode(str[0] == undefined ? "" : str[0]);

                        mainCtrl.regionCode = str[0];
                        //如果分公司不为空 并且分公司pid不是当前大区id，就清空
                        if(bcode && pid != str[0]) {
                            magicBranch.clear();
                            magicStore.clear();
                            //magicBranch.collapse();
                            magicStore.collapse();
                            magicBranch.input.focus();
                        }

                    });
                $(magicRegion2).on(
                    'selectionchange',
                    function(e, cb, s) {
                        var str = cb.getValue(),
                            bcode = $("#magicBranch2 .m-id").val(),
                            pid = $("#magicBranch2 .m-pid").val();
                        getBranchByCode(str[0] == undefined ? "" : str[0], 2);

                        mainCtrl.regionCode2 = str[0];
                        //如果分公司不为空 并且分公司pid不是当前大区id，就清空
                        if(bcode && pid != str[0]) {
                            magicBranch2.clear();
                            magicStore2.clear();
                            //magicBranch.collapse();
                            magicStore2.collapse();
                            magicBranch2.input.focus();
                        }

                    });
                //初始化大区时需要选中第一个进行查询
                if(taskTable == null) {
                    magicRegion.setSelection(allRegionData[0]);
                    opts.settings.searchParam = {
                        "branchCode": "",
                        "cateCode": mainCtrl.taskCategory,
                        "brandCode": mainCtrl.taskBrand,
                        "pageNo": 1,
                        "pageSize": 10,
                        "regionCode": mainCtrl.regionCode,
                        "storeCode": "",
                        "taskId": mainCtrl.taskId,
                        "taskStatus": mainCtrl.taskStatus
                    };
                    taskTable = SwiftUI.swiftTable.getInstance(opts);
                    var intervalSource = setInterval(function() {
                        mainCtrl.dataLen = taskTable.table.total;
                        if(mainCtrl.dataLen > 0) {
                            clearInterval(intervalSource);
                        }
                    }, 100);
                }
                if(taskTable2 == null) {
                    magicRegion2.setSelection(allRegionData[0]);
                    opts2.settings.searchParam = {
                        "branchCode": "",
                        "cateCode": mainCtrl.taskCategory2,
                        "pageNo": 1,
                        "pageSize": 10,
                        "regionCode": mainCtrl.regionCode2,
                        "storeCode": "",
                        "taskId": mainCtrl.taskId
                    }
                    taskTable2 = SwiftUI.swiftTable.getInstance(opts2);
                    var intervalSource2 = setInterval(function() {
                        mainCtrl.dataLen2 = taskTable2.table.total;
                        if(mainCtrl.dataLen2 > 0) {
                            clearInterval(intervalSource2);
                        }
                    }, 100);
                }
                var allBranchData = [];
                for(var i = 0; i < mainCtrl.allBranch.length; i++) {
                    allBranchData.push({
                        id: mainCtrl.allBranch[i].id,
                        name: mainCtrl.allBranch[i].id + "-" + mainCtrl.allBranch[i].text,
                        pid: mainCtrl.allBranch[i].pid
                    });
                }
                magicBranch.setData(allBranchData);
                magicBranch2.setData(allBranchData);
                $(magicBranch).on(
                    'selectionchange',
                    function(e, cb, s) {
                        var str = cb.getValue(),
                            bcode = $("#magicBranch .m-id").val(),
                            bpid = $("#magicBranch .m-pid").val(),
                            scode = $("#magicStore .m-id").val(),
                            spid = $("#magicStore .m-pid").val(),
                            regionid = $("#magicRegion .m-id").val();
                        getStoreByCode(str[0] == undefined ? "" : str[0]);
                        mainCtrl.branchCode = str[0];

                        //如果门店不为空，且门店父id不是当前分公司id  清空
                        if(scode && spid != bcode) {
                            magicStore.clear();
                            magicStore.input.focus();
                        }

                        //如果当前选择分公司的时候 pid 不等于大区id，则要重新设置分公司id
                        if(mainCtrl.regionCode != regionid) {
                            var allRegionList = mainCtrl.allRegion,
                                allRegionLen = allRegionList.length,
                                newRegion = [];
                            if(bpid) {
                                var regionObj = "";
                                for(var i = 0; i < allRegionLen; i++) {
                                    var regionid = allRegionList[i].id;
                                    if(regionid == bpid) {
                                        var regionTxt = allRegionList[i].text,
                                            regionName = regionid + "-" + regionTxt;
                                        regionObj = {
                                            "id": regionid,
                                            "name": regionName,
                                        }
                                        break;
                                    }
                                }
                                if(regionObj != "") {
                                    magicRegion.setSelection(regionObj)
                                }
                            }
                        }
                    });
                $(magicBranch2).on(
                    'selectionchange',
                    function(e, cb, s) {
                        var str = cb.getValue(),
                            bcode = $("#magicBranch2 .m-id").val(),
                            bpid = $("#magicBranch2 .m-pid").val(),
                            scode = $("#magicStore2 .m-id").val(),
                            spid = $("#magicStore2 .m-pid").val(),
                            regionid = $("#magicRegion2 .m-id").val();
                        getStoreByCode(str[0] == undefined ? "" : str[0], 2);
                        mainCtrl.branchCode2 = str[0];

                        //如果门店不为空，且门店父id不是当前分公司id  清空
                        if(scode && spid != bcode) {
                            magicStore2.clear();
                            magicStore2.input.focus();
                        }

                        //如果当前选择分公司的时候 pid 不等于大区id，则要重新设置分公司id
                        if(mainCtrl.regionCode2 != regionid) {
                            var allRegionList = mainCtrl.allRegion,
                                allRegionLen = allRegionList.length,
                                newRegion = [];
                            if(bpid) {
                                var regionObj = "";
                                for(var i = 0; i < allRegionLen; i++) {
                                    var regionid = allRegionList[i].id;
                                    if(regionid == bpid) {
                                        var regionTxt = allRegionList[i].text,
                                            regionName = regionid + "-" + regionTxt;
                                        regionObj = {
                                            "id": regionid,
                                            "name": regionName,
                                        }
                                        break;
                                    }
                                }
                                if(regionObj != "") {
                                    magicRegion2.setSelection(regionObj)
                                }
                            }
                        }
                    });

                var allStoreData = [];
                for(var i = 0; i < mainCtrl.allStore.length; i++) {
                    allStoreData.push({
                        id: mainCtrl.allStore[i].id,
                        name: mainCtrl.allStore[i].id + "-" + mainCtrl.allStore[i].text,
                        pid: mainCtrl.allStore[i].pid
                    });
                }
                magicStore.setData(allStoreData);
                magicStore2.setData(allStoreData);
                $(magicStore).on(
                    'selectionchange',
                    function(e, cb, s) {
                        var str = cb.getValue(),
                            pid = $("#magicStore .m-pid").val(),
                            branchid1 = $("#magicBranch .m-id").val();
                        mainCtrl.storeCode = str[0];
                        //如果当前选择门店的时候 pid 不等于分公司id，则要重新设置分公司id
                        if(pid != branchid1) {
                            var allBranchList = mainCtrl.allBranch,
                                allBranchLen = allBranchList.length,
                                newBranch = [];
                            if(pid) {
                                var branchObj = "";
                                for(var i = 0; i < allBranchLen; i++) {
                                    var branchid = allBranchList[i].id;
                                    if(branchid == pid) {
                                        var branchTxt = allBranchList[i].text,
                                            branchName = branchid + "-" + branchTxt;
                                        branchObj = {
                                            "id": branchid,
                                            "name": branchName,
                                            "pid": allBranchList[i].pid
                                        }
                                        break;
                                    }
                                }
                                if(branchObj != "") {
                                    magicBranch.setSelection(branchObj)
                                }
                            }
                        }

                    });
                $(magicStore2).on(
                    'selectionchange',
                    function(e, cb, s) {
                        var str = cb.getValue(),
                            pid = $("#magicStore2 .m-pid").val(),
                            branchid2 = $("#magicBranch2 .m-id").val();
                        mainCtrl.storeCode2 = str[0];
                        //如果当前选择门店的时候 pid 不等于分公司id，则要重新设置分公司id
                        if(pid != branchid2) {
                            var allBranchList = mainCtrl.allBranch,
                                allBranchLen = allBranchList.length,
                                newBranch = [];
                            if(pid) {
                                var branchObj = "";
                                for(var i = 0; i < allBranchLen; i++) {
                                    var branchid = allBranchList[i].id;
                                    if(branchid == pid) {
                                        var branchTxt = allBranchList[i].text,
                                            branchName = branchid + "-" + branchTxt;
                                        branchObj = {
                                            "id": branchid,
                                            "name": branchName,
                                            "pid": allBranchList[i].pid
                                        }
                                        break;
                                    }
                                }
                                if(branchObj != "") {
                                    magicBranch2.setSelection(branchObj)
                                }
                            }
                        }

                    });
            },
            error: function(err) {
                console.log(err);
            }
        });
    }

    function getAllCategory() {
        $.ajax({
            type: "post",
            dataType: "json",
            data: {
                "taskId": mainCtrl.taskId
            },
            url: DI.queryTaskCateBrands,
            success: function(resp) {
                var data = resp.data;
                if(resp.retFlag != 1) {
                    console.log("获取品类品牌接口错误," + resp.errorMessage)
                    return;
                }
                mainCtrl.allCategoryList = data;

                //初始化默认品类全部，品牌对应也是全部
//              var brandList = [];
//              for(var i in data) {
//                  var category = data[i],
//                      blist = category.brandList.$model;
//                  for(var j in blist) {
//                      var brand = blist[j];
//                      brandList.push(brand);
//                  }
//              }
                var catelist = data;
                catelist.unshift({
                    categoryCode: "",
                    categoryName: "全部",
                    brandList: [{
                        brandCode: "",
                        brandName: "全部"
                    }]
                });
                mainCtrl.categoryList = catelist;
                mainCtrl.brandList = [{
                    brandCode: "",
                    brandName: "全部"
                }];
            },
            error: function() {

            }
        });
    }
    //获取URL数据
    var thiswinurl = window.location.href,
        taskIdStr = "taskId",
        taskIdIdx = thiswinurl.indexOf(taskIdStr);
    //
    if(taskIdIdx != -1) {
        mainCtrl.taskId = thiswinurl.substring(taskIdIdx + (taskIdStr.length + 1));
    }
    //根据大区查公司
    function getBranchByCode(regionCode, type) {
        var allBranchList = mainCtrl.allBranch,
            allBranchLen = allBranchList.length,
            newBranch = [];
        if(regionCode == "") {
            newBranch = allBranchList;
        } else {
            for(var i = 0; i < allBranchLen; i++) {
                if(allBranchList[i].pid == regionCode) {
                    newBranch.push({
                        "id": allBranchList[i].id,
                        "text": allBranchList[i].text,
                        "pid": regionCode
                    });
                }
            }
        }
        var alljsonData = [];
        for(var i = 0; i < newBranch.length; i++) {
            alljsonData.push({
                id: newBranch[i].id,
                name: newBranch[i].id + "-" + newBranch[i].text,
                pid: newBranch[i].pid
            });
        }
        magicBranch.setData(alljsonData);
        if(type == 2) {
            magicBranch2.setData(alljsonData);
        }
    }

    function getStoreByCode(branchCode, type) {
        var allStoreList = mainCtrl.allStore,
            allStoreLen = allStoreList.length,
            newStore = [];
        if(branchCode == "") {
            newStore = allStoreList;
        } else {
            for(var i = 0; i < allStoreLen; i++) {
                if(allStoreList[i].pid == branchCode) {
                    newStore.push({
                        "id": allStoreList[i].id,
                        "text": allStoreList[i].text,
                        "pid": branchCode
                    });
                }
            }
        }
        var alljsonData = [];
        for(var i = 0; i < newStore.length; i++) {
            alljsonData.push({
                id: newStore[i].id,
                name: newStore[i].id + "-" + newStore[i].text,
                pid: newStore[i].pid
            });
        }
        magicStore.setData(alljsonData);
        if(type == 2) {
            magicStore2.setData(alljsonData);
        }
    }
    getAllCategory();

    getAllStores();

    avalon.scan();
});
/*
 * 盘点状态过滤器
 * 0 未盘点 ,1 已提交,2 已结束
 */
avalon.filters.inventoryStatus = function(str, args, args2) {
    var ret = "未开始";
    if(str == 0) {
        ret = "未盘点"
    } else if(str == 1) {
        ret = "已提交";
    } else if(str == 2) {
        ret = "已结束";
    }
    return ret;
};